from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from models import PersonOnEvent



#from django import forms

from django.template import RequestContext
# ...
import datetime, random, sha

#from django.core.mail import send_mail

from CostCalc.models import UserProfile, EventProfile
from CostCalc.forms import LoginForm, RegistrationForm #, EventExpenseForm ,CreateEventForms
#from CostCalc.forms import SetPeopleOnEventForm, SetPeopleOnEventForm, SetDatesOnEventsForm, SubGroupExpenseForm


def homepage(request):
    logout(request)
    return render_to_response('HomePage/HomePage.html', {'foo':'bar',},
                               context_instance=RequestContext(request))
    
def loginpage(request):
    state = "Please log in below..."
    #username = password = ''
    if request.POST:
        username = request.POST.get('username')
        password = request.POST.get('password')
    
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                state = "You're successfully logged in!"
                request.session['user_Id'] = request.user.id
                return HttpResponseRedirect('../../CostCalc/userhomepage')
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."
    form = LoginForm()
    return render_to_response('HomePage/LoginPage.html',{'state':state, 'form':form},#'username': username},
                                                             context_instance=RequestContext(request))

@login_required(login_url='/CostCalc/Login/')
def userhomepage(request):        
    Events = EventProfile.objects.all()
    UserId = request.session['user_Id']
    
    
    return render_to_response('HomePage/userhomepage.html', {'Events':Events},
                           context_instance=RequestContext(request))


    
def register(request):
    #if request.user.is_authenticated():
    #    # They already have an account; don't let them register again
    #    return HttpResponseRedirect('../../CostCalc/Login')
    if request.POST:
        form = RegistrationForm(request.POST)
        if form.is_valid():
            new_user = form.save(form.cleaned_data)
            return HttpResponseRedirect('../../CostCalc/Login')             
        else:
            return render_to_response('Homepage/signup.html', {'form': form},
                               context_instance=RequestContext(request))
    
    form = RegistrationForm()  
    return render_to_response('Homepage/signup.html', {'form': form},
                               context_instance=RequestContext(request))

#@login_required(login_url='/CostCalc/Login/')    
#def CreateEvent(request):
#    if request.POST:
#        form = CreateEventForm(request.POST)
#        if form.is_valid():
#            Event = form.save(form.cleaned_data)
#            return HttpResponseRedirect('/CostCalc/%d/SetPeopleOnEvent'%(Event.id))             
#        else:
#            print form.errors
#            return render_to_response('Homepage/CreateEvent.html', {'form': form},
#                               context_instance=RequestContext(request))
#    
#    form = CreateEventForm()  
#    return render_to_response('Homepage/CreateEvent.html', {'form': form},
#                               context_instance=RequestContext(request))
#
#@login_required(login_url='/CostCalc/Login/')
#def TripHomepage(request, Event_Id):
#    try:
#        Event = EventProfile.objects.get(id=Event_Id)
#    except EventProfile.DoesNotExist:
#        return HttpResponseRedirect('/CostCalc/userhomepage')
#    EventExpenses = Event.eventexpenseprofile_set.all()
#    People = Event.persononevent_set.all()
#    return render_to_response('Homepage/tripHomePage.html', {'Event': Event, 'EventExpenses' : EventExpenses, "People": People},
#                               context_instance=RequestContext(request))
#          
#@login_required(login_url='/CostCalc/Login/')
#def AddTripExpense(request, Event_Id = 0):
#        Event =  EventProfile.objects.get(id=Event_Id)
#        if request.POST:
#            form = EventExpenseForm(request.POST)
#            if form.is_valid():
#                user = request.user
#                Expense = form.save(form.cleaned_data, request.user.id, Event)
#                
#                return HttpResponseRedirect('/CostCalc/%d/%d/ViewTripExpense/'%(Event.id, Expense.id))             
#            else:
#                print form.errors
#                return render_to_response('Homepage/AddTripExpense.html', {'form': form, 'Event':Event},
#                                   context_instance=RequestContext(request))
#    
#        form = EventExpenseForm()  
#        return render_to_response('Homepage/AddTripExpense.html', {'form': form, 'Event':Event},
#                                   context_instance=RequestContext(request))
#
#@login_required(login_url='/CostCalc/Login/')    
#def ViewTripExpense(request, Event_Id, Expense_Id):    
#    Event =  EventProfile.objects.get(id=Event_Id)
#    EventExpense = Event.eventexpenseprofile_set.filter(id = Expense_Id)[0]
#    return render_to_response('Homepage/ViewTripExpense.html', {'EventExpense': EventExpense, 'EventId': Event_Id },
#                               context_instance=RequestContext(request))
#        
#@login_required(login_url='/CostCalc/Login/')
#def SetPeopleOnEvent(request, Event_Id):
#    Event =  EventProfile.objects.get(id=Event_Id)
#    Person = Event.IsIdOnEvent(request.user.userprofile.id)
#    if True:#if Person != None:
#        Users = UserProfile.objects.all()
#        if request.POST:
#            form = SetPeopleOnEventForm(Users, request.POST)
#            if form.is_valid():
#                Ids = form.GetSelectedIds(form.cleaned_data)
#                Event.UpdatePeopleOnEvent(Ids)
#                return HttpResponseRedirect('/CostCalc/%d/TripHomepage'%(Event.id)) 
#            else:
#                return render_to_response('Homepage/SetPeopleOnEventPage.html', {'form': form, 'Event':Event},
#                                   context_instance=RequestContext(request))
#    
#        form = SetPeopleOnEventForm(Users)
#        
#        #form = UserProfile.objects.all()
#        return render_to_response('Homepage/SetPeopleOnEventPage.html', {'form': form, 'Event':Event},
#                                   context_instance=RequestContext(request))
#    else:
#        return HttpResponseRedirect('/CostCalc/userhomepage/')          
#
#@login_required(login_url='/CostCalc/Login/') 
#def SetDatesOnEvent(request, Event_Id, User_Id):
#    Event =  EventProfile.objects.get(id=Event_Id)
#    #Person = Event.IsIdOnEvent(request.user.userprofile.id) Change this to make sure person can edit this persons dets
#    Person = Event.IsIdOnEvent(User_Id)
#    if Person != None:
#        if request.POST:
#            form = SetDatesOnEventsForm(Person.userDatesOnEvent, request.POST)
#            if form.is_valid():
#                Dates = form.GetSelectedDates(form.cleaned_data)
#                Person.UpdateDatesAttended(Dates)
#                return HttpResponseRedirect('/CostCalc/%d/TripHomepage'%(Event.id)) 
#            else:
#                return render_to_response('Homepage/SetDatesOnEventsPage.html', {'form': form, 'Event':Event, 'Person':Person},
#                                   context_instance=RequestContext(request))
#    
#        form = SetDatesOnEventsForm(Person.userDatesOnEvent)
#        
#        #form = UserProfile.objects.all()
#        return render_to_response('Homepage/SetDatesOnEventsPage.html', {'form': form, 'Event':Event, 'Person':Person},
#                                   context_instance=RequestContext(request))
#    else:
#        return HttpResponseRedirect('/CostCalc/userhomepage/')
#        
#@login_required(login_url='/CostCalc/Login/') 
#def CreateSubgroupExpense(request, Event_Id):
#    Event =  EventProfile.objects.get(id=Event_Id)
#    
#    Person = Event.IsIdOnEvent(request.user.userprofile.id)
#    if Person != None:
#        Users = Event.persononevent_set.all()
#        
#        if request.POST:
#            form = SetDatesOnEventsForm(Person.userDatesOnEvent, request.POST)
#            if form.is_valid():
#                Dates = form.GetSelectedDates(form.cleaned_data)
#                Person.UpdateDatesAttended(Dates)
#                return HttpResponseRedirect('/CostCalc/%d/TripHomepage'%(Event.id)) 
#            else:
#                return render_to_response('Homepage/SetDatesOnEventsPage.html', {'form': form, 'Event':Event, 'Person':Person},
#                                   context_instance=RequestContext(request))
#    
#        form = SubGroupExpenseForm(Users)
#        
#        #form = UserProfile.objects.all()
#        return render_to_response('Homepage/SetDatesOnEventsPage.html', {'form': form, 'Event':Event, 'Person':Person},
#                                   context_instance=RequestContext(request))
#    else:
#        return HttpResponseRedirect('/CostCalc/userhomepage/')
        
        
        
        